<?php
/**
 * Trace-Route zur Aufzecihnung von Traces
 * Vor Verwendung TraceRoute mit Yapary_TraceRoute::enable() aktivieren
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2010 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Final Class Yapary_TraceRoute
{
    /** @var bool $enable	:	Feld mit allen gespeicherten Daten */
    static public $enable = false;

    /** @var array $arrData	:	Feld mit allen gespeicherten Daten */
    static private $arrData = array();


    /**
     * Aktiviert das Trace-Logging
     * */
    public static function enable ()
    {
        self::$enable = true;
    }

    /**
     * Deaktiviert das Trace-Logging
     * */
    public static function disable ()
    {
        self::$enable = false;
    }

    /**
     * Erstellt eine Variable
     * @param mixed $_additional : Sonstiges
     * @return void
     * */
    public static function add ($_additional = NULL)
    {
        $arrResult     = array();
        $exception     = new Exception();
        $arrTraceRoute = $exception->getTrace();

        if ($_additional != NULL) {
            $arrTraceRoute[1]['additional'] = $_additional;
        }

        self::$arrData[] = $arrTraceRoute[1];
    }

    /**
     * Wandelt eine Eingabe zu einer konformen Auflistung um
     * @param mixed $arr	: Umzuwandelnde Eingabe
     * @return string
     * */
    private static function toList ($arr) {
        $result = NULL;

        switch(gettype($arr)) {
            case 'array':
                if (count($arr)) {
                    foreach ($arr as $key => $val) {
                        $tmp = self::toList($val);

                        if ($tmp != NULL) {
                            $result .= '<li>param_'.$key.' => '.$tmp.'</li>';
                        }
                    }
                    $result = '[array] =><ul>'.$result.'</ul>';
                }
                break;
            case 'object':
                $result = '[object] '.get_class($arr);
                break;
            case 'string':
                $tmp = strip_tags(preg_replace("/\r|\n/s", "", $arr));
                if ($tmp != '') {
                    $result = '[string] '.$tmp;
                }
                break;
//            default:
//                $result = '<li>['.gettype($arr).'] => '.$arr.'</li>';
//                break;
        }

        return $result;
    }


    /**
     * Liefert alle Traces
     * @return Object
     * */
    public static function display ()
    {
        $result = '';
        $count = count(self::$arrData);
        $style = 'style="border: solid 1px #ccc;"';

        if ($count) {
            foreach (self::$arrData as $key => $val) {
                $index = ($key+1).'['.$count.']';
                $additional = (isset($val['additional'])) ? '<li>[other]     => '.$val['additional'].'</li>' : '';

                $content = '<pre '.$style.'>'
                             . 'Trace '.$index.' {'
                                 . '<ul>'
                                     . '<li>[class]     => '.$val['class'].'</li>'
                                     . '<li>[method]    => '.$val['function'].'</li>'
                                     . '<li>[call file] => '.$val['file'].'</li>'
                                     . '<li>[call line] => '.$val['line'].'</li>'
                                     . '<li>[call args] => '.self::toList($val['args']).'</li>'
                                     . $additional
                                 . '</ul>'
                             . '}'
                         . '</pre>';

                $result .= $content;
            }
        }

        return $result;
    }


}


?>